package com.appkubes.mall.admin.application.goods.sku.controller;

import com.appkubes.common.Response;
import com.appkubes.mall.admin.application.base.BaseController;
import com.appkubes.mall.admin.application.goods.sku.entity.vo.TemplateKeyVO;
import com.appkubes.mall.admin.application.goods.sku.entity.vo.TemplateUpdateVO;
import com.appkubes.mall.admin.application.goods.sku.entity.vo.TemplateVO;
import com.appkubes.mall.admin.application.goods.sku.entity.model.TypeModel;
import com.appkubes.mall.admin.application.goods.sku.service.SpecificAttributeService;
import com.appkubes.mall.admin.application.goods.sku.service.SpecificTypeService;
import com.appkubes.mall.admin.application.goods.sku.service.impl.SpecificTemplateServiceImpl;
import com.appkubes.mall.admin.config.camel.RequestModel;
import com.appkubes.mall.core.entity.MallSpecificTemplate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api("多规格模板")
@RestController
@RequestMapping("/sass_admin/Sspecifictemplate")
public class SpecificTemplateController extends BaseController {
    @Autowired
    SpecificAttributeService specificAttributeService;

    @Autowired
    SpecificTypeService specificTypeService;

    @Autowired
    SpecificTemplateServiceImpl specificTemplateServiceImpl;

    @ApiOperation("获取模版列表")
    @PostMapping("/getAll")
    public Response getAll() {
        return Response.success(specificTemplateServiceImpl.getAll());
    }

    @ApiOperation("添加模版")
    @PostMapping("/add")
    public Response add(@RequestModel TemplateVO template) {
        template.setCategoryKey("GLOBAL");
        MallSpecificTemplate specificTemplate = new MallSpecificTemplate();
        BeanUtils.copyProperties(template, specificTemplate);
        specificTemplateServiceImpl.save(specificTemplate);
        return Response.success(specificTemplate);
    }

    @ApiOperation("更新模板")
    @PostMapping("/update")
    public Response update(@RequestModel TemplateUpdateVO template) {
        MallSpecificTemplate specificTemplate = specificTemplateServiceImpl.getById(template.getSpecificTemplateKey());
        BeanUtils.copyProperties(template, specificTemplate);
        specificTemplateServiceImpl.updateById(specificTemplate);
        return Response.success(specificTemplate);
    }

    @ApiOperation("获取模板")
    @PostMapping("/get")
    public Response get(@RequestParam(defaultValue = "") String key) {
        return Response.success(specificTemplateServiceImpl.getById(key));
    }

    @ApiOperation("删除模板")
    @PostMapping("/del")
    public Response del(@RequestParam(defaultValue = "") String key) {
        return Response.success(specificTemplateServiceImpl.removeById(key));
    }

    @ApiOperation("获取规格组")
    @PostMapping("/getList")
    public Response getList(@RequestModel TemplateKeyVO templateKey) {
        List<TypeModel> typeModels = specificTypeService.getAll(getAppKey(), templateKey.getTemplateKey());
        return Response.success(typeModels);
    }


}
